using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ltf.Remoting.RemoteEvents;
using Ltf.Remoting.Operations;
using Ltf.Remoting.TeamFoundation;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.IO;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Ltf.Remoting.Server;
using Ltf.Remoting.TFAccess;
// some more comments for testing diffs
namespace Ltf.MonitorLizard.Forms
{
	[FormType(FormTypes.MoveTarget)]

	public partial class MoveTarget : Form
	{

		private TFServerConnection connection;

		private Workspace workspace;



		public MoveTarget(string[] pathList, LizardEventArgs args)
		{
			InitializeComponent();
			connection = ServerConnections.GetConnectionForPath(pathList[0]);
			string path = pathList[0];
			workspace = connection.GetWorkspace(path);
			string sourceItem = workspace.GetServerItemForLocalItem(path);
			txtSourcePath.Text = sourceItem;
			string targetPath = Path.GetDirectoryName(sourceItem).Replace('\\', '/');
			string itemName = Path.GetFileName(sourceItem);
			string targetName = targetPath + "/" + itemName;
			
			txtTargetPath.Text = targetName;
			try
			{
				if (workspace.GetLocalItemForServerItem(targetPath) != string.Empty)
				{
					workingFolderPath = workspace.GetLocalItemForServerItem(targetPath);
				}
			}
			catch
			{
				workingFolderPath = string.Empty;
			}
		}


		private string workingFolderPath;

		private void btnSourcePicker_Click(object sender, EventArgs e)
		{
			TFProjectBrowser tfPB = new TFProjectBrowser(txtSourcePath.Text, workspace);
			tfPB.Text = "LizardTF Branch Source";
			DialogResult dr = tfPB.ShowDialog();
			if (dr == DialogResult.OK)
			{
				txtSourcePath.Text = tfPB.ProjectPath;
				if (tfPB.ProjectVersion != 0)
					txtSourcePath.Text = LizardFileAccess.SetPathVersion(txtSourcePath.Text, tfPB.ProjectVersion);
			}
		}

		private void btnTargetPicker_Click(object sender, EventArgs e)
		{
			string targetPath = string.Empty; // Path.GetDirectoryName(txtTargetPath.Text).Replace('\\', '/');
			string targetName = string.Empty; // Path.GetFileName(txtTargetPath.Text);
			try
			{
				targetPath = Path.GetDirectoryName(txtTargetPath.Text).Replace('\\', '/');
			}
			catch { }
			try
			{
				targetName = Path.GetFileName(txtTargetPath.Text);
			}
			catch { }
			TFProjectBrowser tfPB = new TFProjectBrowser(targetPath, true, workspace);
			DialogResult dr = tfPB.ShowDialog();
			if (dr == DialogResult.OK)
			{
				string targetserverPath = tfPB.ProjectPath;
				int v = 0;
				int d = 0;
				Ltf.Remoting.TFAccess.LizardFileAccess.GetNameAndVersionFromPath(targetserverPath, out targetPath, out v, out d);
				txtTargetPath.Text = targetPath + "/" + targetName;
				try
				{
					if (workspace.GetLocalItemForServerItem(targetPath) != string.Empty)
					{
						workingFolderPath = workspace.GetLocalItemForServerItem(targetPath);
					}
				}
				catch
				{
					workingFolderPath = string.Empty;
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnOkay_Click(object sender, EventArgs e)
		{
			char[] invalidChars = Path.GetInvalidPathChars();
			foreach (char c in invalidChars)
			{
				if (txtTargetPath.Text.Contains(c.ToString()))
				{
					MessageBox.Show("File/folder path names can not contain the '" + c.ToString() + "' charactor.", "Invalid Charactor In Name");
					return;
				}
			}


			string targetPath = (txtTargetPath.Text).Replace('\\', '/');
			string sourcePath = (txtSourcePath.Text).Replace('\\', '/');

			if (ServerConnections.GetConnectionForPath(targetPath)!=ServerConnections.GetConnectionForPath(sourcePath))
			{
				MessageBox.Show("LizardTF can not move folders or files between different TFS Servers, or outside to a location outside of a workspace.",
					"Invalid Move Request", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			try
			{
				Item item = workspace.VersionControlServer.GetItem(sourcePath);
			}
			catch
			{
				MessageBox.Show("The source path does not exist, or does not have a working folder in the current workspace.",
					"Source Path Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			try
			{
				Item item = workspace.VersionControlServer.GetItem(Path.GetDirectoryName(targetPath).Replace('\\', '/'));
			}
			catch
			{
				MessageBox.Show("The target path parent does not exist, or does not have a working folder in the current workspace.",
					"Target Path Parent Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}


			string wf = null;
			if (workingFolderPath != string.Empty)
				workspace.TryGetServerItemForLocalItem(workingFolderPath);
			string localTarget = workspace.TryGetLocalItemForServerItem(targetPath);
			int[] workItems = new int[0];

			
			if (localTarget == null)
			{
				MessageBox.Show("The selected target path does not have a local mapping. You must use 'Get local working copy..' option if the target path does not have an existing mapping.",
					"Path Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			WorkspaceOperators wso = new WorkspaceOperators(ServerConnections.GetConnectionForPath(targetPath), workspace);

			wso.PendMove(sourcePath, targetPath);

			this.Close();
		}





		private void txtTargetPath_TextChanged(object sender, EventArgs e)
		{
			try
			{
				string targetPath = Path.GetDirectoryName(txtTargetPath.Text).Replace('\\', '/');
				if (workspace.GetLocalItemForServerItem(targetPath) != string.Empty)
				{
					workingFolderPath = workspace.GetLocalItemForServerItem(targetPath);
				}
			}
			catch
			{
				workingFolderPath = string.Empty;
			}
		}

	}
}